package com.example.understand_compose1.testcase_activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.ActivityFetDataStoreInternalBinding
import java.io.FileOutputStream

class FetDataStoreInternalActivity : AppCompatActivity() {
    lateinit var binding: ActivityFetDataStoreInternalBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetDataStoreInternalBinding.inflate(layoutInflater)

        setContentView(binding.root)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }




    }

    // 数据存储-内部存储,应用卸载数据就同时删除

    fun writeInternal(view: View) {
        val fos:FileOutputStream = openFileOutput("外部文件.txt", MODE_PRIVATE)
        fos.write("Hello world".toByteArray())// fos.write("Hello world".getBytes());
        fos.close()
        Toast.makeText(this, "写入成功: $fos", Toast.LENGTH_SHORT).show()
    }

    fun readInternal(view: View) {
        val fileInputStream = openFileInput("外部文件.txt",)

        val buffer = byteArrayOf(fileInputStream.available().toByte())

        fileInputStream.read(buffer)
        fileInputStream.close()

        Toast.makeText(this, "读出成功: $fileInputStream", Toast.LENGTH_SHORT).show()
    }


}